﻿using System;
using System.Web.Security;

using MvcCms.Service;
using MvcCms.Service.Code.Encryption;
using MvcCms.Service.Logging;
using Microsoft.Practices.ServiceLocation;
using MvcCms.Service.Code;

namespace MvcCms.Web
{
    public partial class FireFile : System.Web.UI.Page
    {
        private readonly IAccountService _accountService;
        public FireFile()
        {
            _accountService = ServiceLocator.Current.GetInstance<IAccountService>();
        }

        public string securityCode = Guid.NewGuid().ToString();
        protected void Page_Load(object sender, EventArgs e)
        {
            //TODO: for now this is the quick and dirty admin gets to do this.  later will need to restrict to the admin's portal
            //and possibly integrate this page into the mvc pages
            var user = _accountService.GetUser();
            string code = "";
            if (user != null)
            {
                if (Roles.IsUserInRole(user.UserName, "admin"))
                {
                    if (string.IsNullOrEmpty(user.FireFileKey))
                    {
                        securityCode = System.Guid.NewGuid().ToString();
                        user.FireFileKey = MvcCmsEncryption.Encrypt(securityCode);
                        if (_accountService.EditUser(user))
                        {
                            if (Request.QueryString["yycode"] == "yySetup")
                            {
                                divConfig.Visible = true;
                            }
                        }
                    }
                    else
                    {
                        securityCode = MvcCmsEncryption.Decrypt(user.FireFileKey);
                    }
                }
                code = Request.Form["code"];
            }

            if (DevCheck.IsDev() && divConfig.Visible == false)
            {
                try
                {
                    var styleSheet = Server.UrlDecode(GetBase64DecodedString(Request.Form["stylesheet"]));
                    var fileName = Server.UrlDecode(GetBase64DecodedString(Request.Form["file"]));
                    var localFilePath = Server.MapPath(new Uri(fileName).AbsolutePath);
                    System.IO.File.WriteAllText(localFilePath, styleSheet);
                    Response.Clear();
                    Response.Write(String.Format("<?xml version='1.0' encoding='ISO-8859-1'?><firefilestatus success='true' msg='FilesSuccessfullySaved' styleindex='{0}' />", Request.Form["index"]));
                    Response.End();
                }
                catch (Exception ex)
                {
                    AppLogging.GetLogger().Error(ex.Message, ex);
                    Response.End();
                }
            }
        }
        private string GetBase64DecodedString(string base64)
        {
            var str = Convert.FromBase64String(base64);
            return System.Text.Encoding.UTF8.GetString(str);
        }
    }
}